package com.example.administrator.schoolblog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.schoolblog.Beans.BlogUser;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends BaseActivity {
    private EditText login_username;
    private EditText login_password;
    private Button blog_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.blog_login);
        Bmob.initialize(this,"b5d62a501dc94902d3f8672a1b5fc2ab","Bmob");
        initView();
    }
    //初始化函数
    private void initView(){
        Button buttonRegister=(Button) findViewById(R.id.blog_login_register);
        TextView getPass=(TextView) findViewById(R.id.blog_login_getpass);
        buttonRegister.setOnClickListener(new OnClickListener());
        getPass.setOnClickListener(new OnClickListener());
        login_username = (EditText) findViewById(R.id.blog_login_username);
        login_password = (EditText) findViewById(R.id.blog_login_password);
        blog_login = (Button) findViewById(R.id.blog_login_login);
        blog_login.setOnClickListener(new OnClickListener());
    }
    //点击事件函数
    private class OnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            String username=login_username.getText().toString().trim();
            String password=login_password.getText().toString().trim();
            switch(v.getId())
            {
                case R.id.blog_login_register:
                    Intent intentRegister = new Intent(LoginActivity.this,RegisterActivity.class);
                    startActivity(intentRegister);
                    break;
                case R.id.blog_login_getpass:
                    Intent intentGetpass = new Intent("com.example.administrator.schoolblog.getpassward.GET_PASSWORD");
                    startActivity(intentGetpass);
                    break;
                case R.id.blog_login_login:
                    if(username.equals("")||password.equals("")){
                        Toast.makeText(LoginActivity.this, "请填写完整登录信息", Toast.LENGTH_SHORT).show();
                    }else {
                        userLogin(username,password);
                    }
                default:
                    break;
            }
        }
    }
    /**
     * 登录函数
     */
    private void userLogin(String username,String password){
        BlogUser bloguser = new BlogUser();
        bloguser.setUsername(username);
        bloguser.setPassword(password);
        bloguser.login(new SaveListener<BlogUser>() {
            @Override
            public void done(BlogUser blogUser, BmobException e) {
                if(e==null){
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    BlogUser login_user = BlogUser.getCurrentUser(BlogUser.class);
                    String userId = login_user.getObjectId();
                    Intent toMainIntent = new Intent(LoginActivity.this,MainActivity.class);
                    toMainIntent.putExtra("UserId",userId);
                    startActivity(toMainIntent);
                }
                else{
                    Toast.makeText(LoginActivity.this, "登录失败："+e.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
